package com.HRs.UI;

import com.HRs.Controller.PatientController;
import com.HRs.Controller.aDController;
import com.HRs.Controller.dtAController;
import com.HRs.domain.Department;
import com.HRs.domain.Dtadministrator;
import com.HRs.domain.Patient;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class Hall_Admin {
    JFrame frame = new JFrame("管理员后台");

    JPanel panel1;
    JPanel panel2;
    JPanel panel3;

    JTable table; //医生
    JScrollPane sc;
    String[][] table_dtA = new String[100][100];
    String[] columnId_dtA = {"医生编号","姓名", "科室", "性别", "联系电话", "密码"};
    DefaultTableModel tableModel;

    JTable table2; //病人
    JScrollPane sc2;
    String[][] table_pat = new String[100][100];
    String[] columnId_pat = {"病人编号","姓名", "密码", "性别", "电话", "地址", "身份证号"};
    DefaultTableModel tableModel2;

    JTable table3; //科室信息
    JScrollPane sc3;
    String[][] table_dp = new String[100][100];
    String[] columnId_dp = {"科室编号","科室名称", "科室记录"};
    DefaultTableModel tableModel3;

    final int H = 770, W = 720;
    int selected_row;
    int selected_col;

    public Hall_Admin(){
        frame.setLayout(null);

        JPanel p1 = new JPanel();
        p1.setBounds(0, 0, 230 ,80);
        p1.setBackground(Color.DARK_GRAY);
        JLabel l1 = new JLabel("后台管理");
        l1.setFont(new Font("隶书", Font.PLAIN, 35));
        l1.setForeground(Color.WHITE);
        p1.add(BorderLayout.CENTER, l1);
        frame.add(p1);

        JPanel p2 = new JPanel();
        p2.setBounds(230, 0, 770, 80);
        p2.setBackground(Color.GRAY);
        JLabel l2 = new JLabel("超级管理员后台");
        l2.setFont(new Font("隶书", Font.PLAIN, 35));
        l2.setForeground(Color.WHITE);
        p2.add(l2);
        frame.add(p2);

        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(8, 1));
        p3.setBackground(Color.GRAY);
        p3.setBounds(0, 80, 230, 800);

        JButton b_dtA = new JButton("科室管理员信息");
        b_dtA.setFont(new Font("隶书", Font.PLAIN, 25));
        b_dtA.setBounds(0, 0, 200, 50);
        b_dtA.setContentAreaFilled(false);
        p3.add(b_dtA);

        JButton b_pat = new JButton("病人信息");
        b_pat.setFont(new Font("隶书", Font.PLAIN, 25));
        b_pat.setBounds(0, 50, 200, 50);
        b_pat.setContentAreaFilled(false);
        p3.add(b_pat);

        JButton b_Dp = new JButton("科室信息管理");
        b_Dp.setFont(new Font("隶书", Font.PLAIN, 25));
        b_Dp.setBounds(0, 50, 200, 50);
        b_Dp.setContentAreaFilled(false);
        p3.add(b_Dp);

        JButton b_Return = new JButton("返回登录界面");
        b_Return.setFont(new Font("隶书", Font.PLAIN, 25));
        b_Return.setBounds(0, 150, 200, 50);
        b_Return.setContentAreaFilled(false);
        b_Return.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login();
                frame.dispose();
            }
        });
        p3.add(b_Return);
        frame.add(p3);

        dtA();
        pat();
        dp();

        b_dtA.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel2.setVisible(false);
                panel3.setVisible(false);
                panel1.setVisible(true);
            }
        });

        b_pat.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel1.setVisible(false);
                panel3.setVisible(false);
                panel2.setVisible(true);
            }
        });

        b_Dp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel1.setVisible(false);
                panel2.setVisible(false);
                panel3.setVisible(true);
            }
        });

        frame.setSize(1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void dtA(){
        panel1 = new JPanel();
        panel1.setBounds(230, 80, H, W);
        panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));

        JPanel t2 = new JPanel();

        get_dta(table_dtA);

        tableModel = new DefaultTableModel(table_dtA, columnId_dtA);
        table = new JTable(tableModel);
        table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table.getTableHeader().setFont(new Font("隶书",Font.PLAIN,16));
        sc = new JScrollPane(table);
        t2.add(sc);
        panel1.add(t2);

        JPanel t3 = new JPanel();
        JLabel lblId = new JLabel("医生编号:");
        lblId.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtId = new JTextField(10);
        t3.add(lblId);
        t3.add(txtId);
        JLabel lblName = new JLabel("姓名:");
        lblName.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtName = new JTextField(10);
        t3.add(lblName);
        t3.add(txtName);
        JLabel lblDp = new JLabel("科室:");
        lblDp.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtDp = new JTextField(10);
        t3.add(lblDp);
        t3.add(txtDp);
        JPanel t1 = new JPanel();

        JLabel lblSex = new JLabel("性别:");
        lblSex.setFont(new Font("隶书", Font.PLAIN, 15));
        JComboBox<String> boxSex = new JComboBox<>();
        boxSex.setFont(new Font("隶书", Font.PLAIN, 15));
        boxSex.addItem("-选择性别-");
        boxSex.addItem("男");
        boxSex.addItem("女");
        t1.add(lblSex);
        t1.add(boxSex);
        JLabel lblNum = new JLabel("电话:");
        lblNum.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtNum = new JTextField(12);
        t1.add(lblNum);
        t1.add(txtNum);
        JLabel lblPs = new JLabel("密码:");
        lblPs.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtPs = new JTextField(10);
        t1.add(lblPs);
        t1.add(txtPs);
        panel1.add(t3);
        panel1.add(t1);

        JPanel t4 = new JPanel();
        JButton b_delete = new JButton("删除");
        JButton b_update = new JButton("修改");
        JButton b_insert = new JButton("添加");
        JButton b_Flush = new JButton("刷新");
        b_delete.setFont(new Font("隶书", Font.ITALIC, 20));
        b_insert.setFont(new Font("隶书", Font.ITALIC, 20));
        b_update.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setContentAreaFilled(false);
        b_delete.setContentAreaFilled(false);
        b_insert.setContentAreaFilled(false);
        b_update.setContentAreaFilled(false);
        t4.add(b_insert);
        t4.add(b_delete);
        t4.add(b_update);
        t4.add(b_Flush);

        b_Flush.addActionListener(new ActionListener() {//刷新
            @Override
            public void actionPerformed(ActionEvent e) {
                flush_daT();
            }
        });

        b_delete.addActionListener(new ActionListener() {//删除
            @Override
            public void actionPerformed(ActionEvent e) {
                dtAController dC = new dtAController();
                dC.delete_row(selected_row);
                flush_daT();
            }
        });

        b_insert.addActionListener(new ActionListener() {//添加
            @Override
            public void actionPerformed(ActionEvent e) {
                dtAController dC = new dtAController();
                String id, name, depart, sex, num, pass;
                id = txtId.getText();
                name = txtName.getText();
                sex = (String) boxSex.getSelectedItem();
                num = txtNum.getText();
                pass = txtPs.getText();
                depart = txtDp.getText();

                Dtadministrator dta = new Dtadministrator(id, name, depart, sex, num, pass);
                dC.insert(dta);
                flush_daT();
            }
        });

        b_update.addActionListener(new ActionListener() {//修改
            @Override
            public void actionPerformed(ActionEvent e) {
                dtAController dC = new dtAController();
                String id, name, depart, sex, num, pass;
                id = (String) table.getValueAt(selected_row, 0);
                name = (String) table.getValueAt(selected_row, 1);
                depart = (String) table.getValueAt(selected_row, 2);
                sex = (String) table.getValueAt(selected_row, 3);
                num = (String) table.getValueAt(selected_row, 4);
                pass = (String) table.getValueAt(selected_row, 5);
                String[] information = {id, name, depart, sex, num, pass};
                dC.delete_row(selected_row);
                Dtadministrator dta = new Dtadministrator(id, name, depart, sex, num, pass);
                tableModel.insertRow(selected_row, information);
                dC.insert(dta);
                flush_daT();
            }
        });

        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selected_row = table.getSelectedRow();
                selected_col = table.getSelectedColumn();
            }
        });

        table.addMouseListener(new MouseAdapter() {
        });

        panel1.add(t4);

        frame.add(panel1);
    }

    public void pat(){
        panel2 = new JPanel();
        panel2.setBounds(230, 80, H, W);
        panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));

        JPanel t2 = new JPanel();

        get_pat(table_pat);

        tableModel2 = new DefaultTableModel(table_pat, columnId_pat);
        table2 = new JTable(tableModel2);
        table2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table2.getTableHeader().setFont(new Font("隶书",Font.PLAIN,16));
        sc2 = new JScrollPane(table2);
        t2.add(sc2);
        panel2.add(t2);

        JPanel t3 = new JPanel();
        JLabel lblId = new JLabel("病人编号:");
        lblId.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtId = new JTextField(10);
        t3.add(lblId);
        t3.add(txtId);
        JLabel lblName = new JLabel("姓名:");
        lblName.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtName = new JTextField(10);
        t3.add(lblName);
        t3.add(txtName);
        JLabel lblAdr = new JLabel("地址:");
        lblAdr.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtDp = new JTextField(10);
        t3.add(lblAdr);
        t3.add(txtDp);

        JPanel t1 = new JPanel();
        JLabel lblSex = new JLabel("性别:");
        lblSex.setFont(new Font("隶书", Font.PLAIN, 15));
        JComboBox<String> boxSex = new JComboBox<>();
        boxSex.setFont(new Font("隶书", Font.PLAIN, 15));
        boxSex.addItem("-选择性别-");
        boxSex.addItem("男");
        boxSex.addItem("女");
        t1.add(lblSex);
        t1.add(boxSex);
        JLabel lblNum = new JLabel("电话:");
        lblNum.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtNum = new JTextField(12);
        t1.add(lblNum);
        t1.add(txtNum);
        JLabel lblPs = new JLabel("密码:");
        lblPs.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtPs = new JTextField(8);
        t1.add(lblPs);
        t1.add(txtPs);
        JLabel lblID = new JLabel("身份证号:");
        lblID.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtID = new JTextField(18);
        t1.add(lblID);
        t1.add(txtID);
        panel2.add(t3);
        panel2.add(t1);

        JPanel t4 = new JPanel();
        JButton b_delete = new JButton("删除");
        JButton b_update = new JButton("修改");
        JButton b_insert = new JButton("添加");
        JButton b_Flush = new JButton("刷新");
        b_delete.setFont(new Font("隶书", Font.ITALIC, 20));
        b_insert.setFont(new Font("隶书", Font.ITALIC, 20));
        b_update.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setContentAreaFilled(false);
        b_delete.setContentAreaFilled(false);
        b_insert.setContentAreaFilled(false);
        b_update.setContentAreaFilled(false);
        t4.add(b_insert);
        t4.add(b_delete);
        t4.add(b_update);
        t4.add(b_Flush);

        b_Flush.addActionListener(new ActionListener() {//刷新
            @Override
            public void actionPerformed(ActionEvent e) {
                flush_pat();
            }
        });

        b_delete.addActionListener(new ActionListener() {//删除
            @Override
            public void actionPerformed(ActionEvent e) {
                PatientController PC = new PatientController();
                PC.delete_row(selected_row);
                flush_pat();
            }
        });

        b_insert.addActionListener(new ActionListener() {//添加
            @Override
            public void actionPerformed(ActionEvent e) {
                PatientController PC = new PatientController();
                String pid, name, pass, sex, num, adr, Id;
                pid = txtId.getText();
                name = txtName.getText();
                pass = txtPs.getText();
                sex = (String) boxSex.getSelectedItem();
                num = txtNum.getText();
                adr = txtDp.getText();
                Id = txtID.getText();

                Patient pat = new Patient(pid, name, pass, sex, num, adr, Id);
                PC.insert(pat);
                flush_pat();
            }
        });

        b_update.addActionListener(new ActionListener() {//修改
            @Override
            public void actionPerformed(ActionEvent e) {
                PatientController PC = new PatientController();
                String pid, name, pass, sex, num, adr, Id;
                pid = (String) table2.getValueAt(selected_row, 0);
                name = (String) table2.getValueAt(selected_row, 1);
                pass = (String) table2.getValueAt(selected_row, 2);
                sex = (String) table2.getValueAt(selected_row, 3);
                num = (String) table2.getValueAt(selected_row, 4);
                adr = (String) table2.getValueAt(selected_row, 5);
                Id = (String) table2.getValueAt(selected_row, 6);
                String[] information = {pid, name, sex, num, pass, Id, adr};
                PC.delete_row(selected_row);
                Patient pat = new Patient(pid, name, pass, sex, num, adr, Id);
                tableModel2.insertRow(selected_row, information);
                PC.insert(pat);
                flush_pat();
            }
        });

        table2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selected_row = table2.getSelectedRow();
                selected_col = table2.getSelectedColumn();
            }
        });

        table2.addMouseListener(new MouseAdapter() {
        });

        panel2.add(t4);
        panel2.setVisible(false);
        frame.add(panel2);
    }

    public void dp(){
        panel3 = new JPanel();
        panel3.setBounds(230, 80, H, W);
        panel3.setLayout(new BoxLayout(panel3,BoxLayout.Y_AXIS));

        JPanel t2 = new JPanel();
        get_dp(table_dp);
        tableModel3 = new DefaultTableModel(table_dp, columnId_dp);
        table3 = new JTable(tableModel3);
        table3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table3.getTableHeader().setFont(new Font("隶书",Font.PLAIN,18));
        sc3 = new JScrollPane(table3);
        t2.add(sc3);
        panel3.add(t2);

        JPanel t3 = new JPanel();
        JLabel lblId = new JLabel("科室编号:");
        lblId.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtId = new JTextField(10);
        t3.add(lblId);
        t3.add(txtId);
        JLabel lblName = new JLabel("科室名称:");
        lblName.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtName = new JTextField(10);
        t3.add(lblName);
        t3.add(txtName);
        JLabel lblRe = new JLabel("科室记录:");
        lblRe.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtRe = new JTextField(10);
        t3.add(lblRe);
        t3.add(txtRe);

        panel3.add(t3);

        JPanel t4 = new JPanel();
        JButton b_delete = new JButton("删除");
        JButton b_update = new JButton("修改");
        JButton b_insert = new JButton("添加");
        JButton b_Flush = new JButton("刷新");
        b_delete.setFont(new Font("隶书", Font.ITALIC, 20));
        b_insert.setFont(new Font("隶书", Font.ITALIC, 20));
        b_update.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setContentAreaFilled(false);
        b_delete.setContentAreaFilled(false);
        b_insert.setContentAreaFilled(false);
        b_update.setContentAreaFilled(false);
        t4.add(b_insert);
        t4.add(b_delete);
        t4.add(b_update);
        t4.add(b_Flush);

        b_Flush.addActionListener(new ActionListener() {//刷新
            @Override
            public void actionPerformed(ActionEvent e) {
                flush_dp();
            }
        });

        b_delete.addActionListener(new ActionListener() {//删除
            @Override
            public void actionPerformed(ActionEvent e) {
                aDController aD = new aDController();
                aD.deleteDp_row(selected_row);
                flush_dp();
            }
        });

        b_insert.addActionListener(new ActionListener() {//添加
            @Override
            public void actionPerformed(ActionEvent e) {
                aDController aD = new aDController();
                String id, name, re;
                id = txtId.getText();
                name = txtName.getText();
                re = txtRe.getText();
                aD.insertDp(new Department(id, name, re));
                flush_dp();
            }
        });

        b_update.addActionListener(new ActionListener() {//修改
            @Override
            public void actionPerformed(ActionEvent e) {
                aDController aD = new aDController();
                String id, name, re;
                id = (String) table3.getValueAt(selected_row, 0);
                name = (String) table3.getValueAt(selected_row, 1);
                re = (String) table3.getValueAt(selected_row,2);
                String[] information = {id, name, re};
                aD.deleteDp_row(selected_row);
                tableModel3.insertRow(selected_row, information);
                aD.insertDp(new Department(id, name, re));
                flush_dp();
            }
        });

        table3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selected_row = table3.getSelectedRow();
                selected_col = table3.getSelectedColumn();
            }
        });

        table3.addMouseListener(new MouseAdapter() {
        });

        panel3.add(t4);
        panel3.setVisible(false);
        frame.add(panel3);
    }

    public void get_dta(String[][] t_dtA){
        ArrayList<Dtadministrator> dtA = new ArrayList<Dtadministrator>();
        dtAController dC = new dtAController();

        dtA = dC.get_Dta();

        for(int i = 0; i < dtA.size(); i ++ ){
            t_dtA[i][0] = dtA.get(i).getDta_Id();
            t_dtA[i][1] = dtA.get(i).getDta_Name();
            t_dtA[i][2] = dtA.get(i).getDta_Department();
            t_dtA[i][3] = dtA.get(i).getDta_Sex();
            t_dtA[i][4] = dtA.get(i).getDta_ContactNumber();
            t_dtA[i][5] = dtA.get(i).getDta_Password();
        }
    }

    public void get_pat(String[][] t_pat){
        ArrayList<Patient> pat = new ArrayList<Patient>();
        PatientController PC = new PatientController();

        pat = PC.get_Dta();

        for(int i = 0; i < pat.size(); i ++ ){
            t_pat[i][0] = pat.get(i).getP_Pid();
            t_pat[i][1] = pat.get(i).getP_Name();
            t_pat[i][2] = pat.get(i).getP_Password();
            t_pat[i][3] = pat.get(i).getP_Sex();
            t_pat[i][4] = pat.get(i).getP_PhoneNumber();
            t_pat[i][5] = pat.get(i).getP_Address();
            t_pat[i][6] = pat.get(i).getP_ID();
        }
    }

    public void flush_daT(){
        String[][] table_F = new String[100][100];
        get_dta(table_F);

        DefaultTableModel dtm = (DefaultTableModel) table.getModel();
        dtm.setDataVector(table_F, columnId_dtA);
        table.updateUI();
    }

    public void flush_pat(){
        String[][] table_F = new String[100][100];
        get_pat(table_F);

        DefaultTableModel dtm = (DefaultTableModel) table2.getModel();
        dtm.setDataVector(table_F, columnId_pat);
        table2.updateUI();
    }

    public void get_dp(String[][] t_dp){
        ArrayList<Department> adp = new ArrayList<>();
        aDController aD = new aDController();
        adp = aD.get_Dp();

        for(int i = 0; i < adp.size(); i ++ ){
            t_dp[i][0] = adp.get(i).getDp_Id();
            t_dp[i][1] = adp.get(i).getDp_Name();
            t_dp[i][2] = adp.get(i).getDp_remarks();
        }
    }

    public void flush_dp(){
        String[][] table_F = new String[100][100];
        get_dp(table_F);

        DefaultTableModel dtm = (DefaultTableModel) table3.getModel();
        dtm.setDataVector(table_F, columnId_dp);
        table3.updateUI();
    }

    public static void main(String[] args) {
        new Hall_Admin();
    }

}


